package com.mxchip.livestar.base

import android.app.Application
import android.util.Log
import androidx.lifecycle.*
import com.mxchip.livestar.repository.CallLogRepository
import com.mxchip.livestar.utils.WordToPinYinUtils
import com.mxchip.livestar.repository.ContactRepository

class ContactViewModel(application: Application) : AndroidViewModel(application) {

    private val contactRepository: ContactRepository = ContactRepository.getInstance(application.applicationContext)
    private val callLogRepository: CallLogRepository = CallLogRepository.getInstance(application.applicationContext)

    private val _contactFilterList: MediatorLiveData<List<ContactPOJO>> by lazy {
        MediatorLiveData<List<ContactPOJO>>().also {
            it.value = arrayListOf()
        }
    }

    val contactFilterList : LiveData<List<ContactPOJO>>
        get() = _contactFilterList

    private val contactAll: LiveData<List<ContactPOJO>> by lazy {
        contactRepository.all
    }

    private val searchWord: MutableLiveData<String> by lazy {
        MutableLiveData<String>(null)
    }

    private val contactFilterListObserver: Observer<Any> = Observer {

        Log.e("ContactViewModel", "contactFilterListObserver changed: contactAll: ${contactAll.value}")

        val keyword = searchWord.value

        if (keyword.isNullOrBlank()) {
            _contactFilterList.value = contactAll.value
            return@Observer
        }

        val list = contactAll.value?.filter { contact->
            val pingyin = contact.pys

            if (pingyin != null) {
                if (pingyin.contains(keyword)) {
                    return@filter true
                }
            }
            val name = contact.contactName
            if (name != null) {
                if (name.contains(keyword)) {
                    return@filter true
                }
                val pingy = WordToPinYinUtils.getInstance().getSelling(name)
                if (!pingy.isNullOrEmpty()
                        && pingy.contains(keyword)) {
                    return@filter true
                }
            }
            val number = contact.contactNumber
            if (number != null) {
                if (number.contains(keyword)) {
                    return@filter true
                }
            }
            return@filter false
        }

        _contactFilterList.value = list
    }


    init {
        _contactFilterList.addSource(contactAll, contactFilterListObserver)
        _contactFilterList.addSource(searchWord, contactFilterListObserver)
    }

    fun searchWord(s: String?) {
        searchWord.value = s
    }

    /*fun searchWord(): LiveData<String> {
        return searchWord
    }*/

    fun createContact(name: String, number: String) {
        contactRepository.insert(name, number)
        callLogRepository.reload()
    }

    fun updateContact(rawContactId: Long, name: String, number: String) {
        contactRepository.update(rawContactId, name, number)
        callLogRepository.reload()
    }

    fun deleteContactByRawContactId(rawContactId: Long) {
        contactRepository.deleteById(rawContactId)
        callLogRepository.reload()
    }

}